<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
	//项目的发布路径，例如:  /shxt
	String path = request.getContextPath();
	/*
	全路径，形式如下: http://127.0.0.1:8888/shxt/
	request.getScheme()      ——> http 获取协议
	request.getServerName()  --> 127.0.0.1 获取服务名
	request.getServerPort()  --> 8888 获取端口号
	path                     --> /shxt 获取访问的路径 路
	*/
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<meta charset="UTF-8">
<title>注册页面</title>
    <link rel="stylesheet" href="css/register.css">
    <script src="js/locationUtil.js" type="module" charset="utf-8"></script>
    <script>

    function Focus(temp) {
        document.querySelector(`.${temp}_error_msg`).innerHTML = "";
        document.querySelector(`.${temp}_error_img`).classList.remove("errorImg");
        document.querySelector(`.${temp}_error_msg`).classList.remove("errorMessage");
        document.querySelector(`.${temp}_error_msg`).classList.remove("okMessage");
        document.querySelector(`.${temp}_error_img`).classList.remove("okImg");
        document.querySelector(`.${temp}_error_img`).classList.remove("okImg");
    }
    function errorBlur(temp) {
        document.querySelector(`.${temp}_error_msg`).classList.add("errorMessage");
        document.querySelector(`.${temp}_error_img`).classList.add("errorImg");
    }
    function okBlur(temp) {
        document.querySelector(`.${temp}_error_msg`).classList.add("okMessage");
        document.querySelector(`.${temp}_error_img`).classList.add("okImg");
    }

    function userNameFocus() {
        Focus('userName');
    }
    function userNameBlur() {
        let regExp = /^\w+$/;
        let userName = document.getElementById("userName").value;
        if (!regExp.test(userName)) {
            document.querySelector(".userName_error_msg").innerHTML = "用户名格式不正确！";
            errorBlur('userName');
        } else {
        	let xhr = new XMLHttpRequest();
        	let url="RegisterServlet.do?name="+userName;
        	xhr.responseType="json";
        	xhr.open("POST",url,true);
        	xhr.addEventListener("readystatechange",()=>{
        		if (xhr.readyState == 4 && xhr.status == 200) {
        			let json = xhr.response;
        			if (json.result == "ok") {
            			console.log(json.name);
            		}
        		}
        	},false);
        	xhr.send(null);
        	
            document.querySelector(".userName_error_msg").innerHTML = "用户名格式符合要求！";
            okBlur('userName');
        }
    }

    function userNickFocus() {
        Focus('userNick');
    }
    function userNickBlur() {
        let userNick = document.getElementById("userNick").value;
        if (userNick==""|| userNick==null) {
            document.querySelector(".userNick_error_msg").innerHTML = "昵称格式不能为空！";
            errorBlur('userNick');
        } else {
            document.querySelector(".userNick_error_msg").innerHTML = "昵称格式符合要求！";
            okBlur('userNick');
        }
    }

    function passwordFocus() {
        Focus('password');
    }
    function passwordBlur() {
        let regExp = /^\w{8,20}$/;
        let password = document.getElementById("password").value;
        if (!regExp.test(password)) {
            document.querySelector(".password_error_msg").innerHTML = "密码格式不正确！";
            errorBlur('password');
        } else {
            document.querySelector(".password_error_msg").innerHTML = "密码格式符合要求！";
            okBlur('password');
        }
    }

    function rePasswordFocus() {
        Focus('rePassword');
    }
    function rePasswordBlur() {
        let rePassword = document.getElementById("rePassword").value;
        let password = document.getElementById("password").value;
        if (password != rePassword) {
            document.querySelector(".rePassword_error_msg").innerHTML = "两次输入的密码请一致！";
            errorBlur('rePassword');
        } else {
            document.querySelector(".rePassword_error_msg").innerHTML = "密码格式符合要求！";
            okBlur('rePassword');
        }
    }

    function phoneFocus() {
        Focus('phone');
    }
    function phoneBlur() {
        let regExp = /^1\d{10}$/;
        let phone = document.getElementById("phone").value;
        if (!regExp.test(phone)) {
            document.querySelector(".phone_error_msg").innerHTML = "手机号格式不正确！";
            errorBlur('phone');
        } else {
            document.querySelector(".phone_error_msg").innerHTML = "手机号格式符合要求！";
            okBlur('phone');
        }
    }

    
    function emailFocus(){
        Focus('email')
    }
    function emailBlur(){
        let regExp = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;
        if (!regExp.test(document.getElementById("email").value)) {
            document.querySelector(".email_error_msg").innerHTML = "邮箱格式不正确！";
            errorBlur('email');
        } else {
            document.querySelector(".email_error_msg").innerHTML = "邮箱格式符合要求！";
            okBlur('email');
        }
    }



    function init() {
            //email检验置空
            document.getElementById("email").addEventListener("focus", emailFocus, false);
            //email检验
            document.getElementById("email").addEventListener("blur", emailBlur, false);

            //userName检验置空
            document.getElementById("userName").addEventListener("focus", userNameFocus, false);
            //userName检验
            document.getElementById("userName").addEventListener("blur", userNameBlur, false);

            //userNick检验置空
            document.getElementById("userNick").addEventListener("focus", userNickFocus, false);
            //userNick检验
            document.getElementById("userNick").addEventListener("blur", userNickBlur, false);

            //password检验置空
            document.getElementById("password").addEventListener("focus", passwordFocus, false);
            //password检验
            document.getElementById("password").addEventListener("blur", passwordBlur, false);

            //rePassword检验置空
            document.getElementById("rePassword").addEventListener("focus", rePasswordFocus, false);
            //rePassword检验
            document.getElementById("rePassword").addEventListener("blur", rePasswordBlur, false);

            //phone检验置空
            document.getElementById("phone").addEventListener("focus", phoneFocus, false);
            //phone检验
            document.getElementById("phone").addEventListener("blur", phoneBlur, false);

        }
        window.addEventListener("load", init, false);
    
    </script>
</head>
<body>
    <section>
        <aside class="img_box"></aside>
        <h1 style="text-align:center;margin-top:30px;">用户注册</h1>
        <fieldset>
            <legend hidden>用户注册</legend>
            <form action="UserServlet.do" method="POST">
            	<input type="hidden" name="param" value="doRegister">
                <table>
                    <tbody>
                        <tr>
                            <td><label for="userName">用户名：</label></td>
                            <td><input type="text" name="userName" id="userName" autofocus></td>
                            <td>
                                <nav class="error_block">
                                    <div class="userName_error_img"></div>
                                    <div class="userName_error_msg"></div>
                                </nav>
                            </td>
                        </tr>
                        <tr>
                            <td><label for="userNick">昵称：</label></td>
                            <td><input type="text" name="nickName" id="userNick" ></td>
                            <td>
                                <nav class="error_block">
                                    <div class="userNick_error_img"></div>
                                    <div class="userNick_error_msg"></div>
                                </nav>
                            </td>
                        </tr>
                        <tr>
                            <td><label for="password">密码：</label></td>
                            <td><input type="password" name="password" id="password"></td>
                            <td>
                                <nav class="error_block">
                                    <div class="password_error_img"></div>
                                    <div class="password_error_msg"></div>
                                </nav>
                            </td>
                        </tr>
                        <tr>
                            <td><label for="rePassword">再输入一次：</label></td>
                            <td><input type="password" name="rePassword" id="rePassword"></td>
                            <td>
                                <nav class="error_block">
                                    <div class="rePassword_error_img"></div>
                                    <div class="rePassword_error_msg"></div>
                                </nav>
                            </td>
                        </tr>
                        <tr>
                            <td><label for="email">email：</label></td>
                            <td><input type="email" name="email" id="email"></td>
                            <td>
                                <nav class="error_block">
                                    <div class="email_error_img"></div>
                                    <div class="email_error_msg"></div>
                                </nav>
                            </td>
                        </tr>
                        <!-- <tr>
                            <td><label for="phone">手机号：</label></td>
                            <td>
                                <input type="text" name="phone" id="phone">
                            </td>
                            <td>
                                <nav class="error_block">
                                    <div class="phone_error_img"></div>
                                    <div class="phone_error_msg"></div>
                                </nav>
                            </td>
                        </tr> -->
                        <tr>
                            <td>性别：</td>
                            <td colspan="2">
                                <input type="radio" name="sex" value="1" id="man" checked>
                                <label for="man">男</label>&nbsp;
                                <input type="radio" name="sex" value="2" id="woman">
                                <label for="woman">女</label>
                            </td>
                        </tr>
                        <tr>
								<td>爱好：</td>
								<td colspan="2">
								<table>
									<tr>
										<c:forEach items="${requestScope.hobbies}" var="hobby" varStatus="status">
											<td>
												<input type="checkbox" name="fav" id="${hobby.code}" value="${hobby.code}" />
												<label for="${hobby.code}">${hobby.name}</label>
											<td>
											<c:if test="${(status.index+1)%4==0}"></tr><tr></c:if>
										</c:forEach>
									</tr>
								</table>
								</td>
							</tr>
							<tr>
								<td>来自于：</td>
								<td colspan="2">
									<select id="selProvince">
									<c:forEach items="${requestScope.provinces}" var="province">
										<option value="${province.code}">${province.name}</option>
									</c:forEach>
									</select>&nbsp;&nbsp;&nbsp;&nbsp;
									<select id="selCity" name="cityCode"></select>
								</td>
							</tr>
                       <!--  <tr>
                            <td>照片：</td>
                            <td colspan="2">
                                <input type="file" name="photo" accept="image/*">
                            </td>
                        </tr>
                        <tr>
                            <td>生日：</td>
                            <td colspan="2">
                                <input type="date" name="birthday">
                            </td>
                        </tr>
                        <tr> -->
                            <td colspan="3">
                                <nav style="text-align: center;margin-top:30px;">
                                    <button type="reset">重置</button>&nbsp;&nbsp;
                                    <button type="submit">注册</button>
                                </nav>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </form>
        </fieldset>
    </section>
</body>

</html>